<?php
session_start();
if (!class_exists("Config")) require_once "classes/class.Config.php";
$cfg = new config();

switch ($_GET['m']) {
    
    // Iniciar Sesion
    case "iniciar":

            if (!class_exists("Config"))    include_once 'classes/class.Config.php';
            $cfg = new config;
            if (!class_exists("DataBase"))    include_once 'classes/class.DataBase.php';
            if (!class_exists("Usuario"))     include_once 'classes/class.Usuario.php';

            $bd = new DataBase();

            $mail = $_POST['mail'];
            $pass = $_POST['password'];

            $user = new Usuario();
            $user->setMail($mail);
            $user->getFromBBDDByMail();

            if ($bd->checkLogin($mail, $pass)) 
            {
            //    echo "WIN!<br>";
                $_SESSION['id']        = $user->getId();
                $_SESSION['nombre']    = $user->getNombre();
                $_SESSION['apellidos'] = $user->getApellidos();
                $_SESSION['mail']      = $user->getMail();     
                $_SESSION['fecha']     = date("Y-m-d H:i:s");
                $_SESSION['logged']    = true;
            }
            else{
            //    echo "FAIL!<br>";
                session_destroy(); //destruimos la session previamente creada.
            }
            //redireccionamos.
            header("location: {$cfg->urlBase}");

            break;
        
        
    case "cerrar":
        
            session_destroy();
            header("location: {$cfg->urlBase}");
            break;
    
    case "iniciar_admin":
        
            if (!class_exists("Config"))    include_once 'classes/class.Config.php';
            $cfg = new config;
            if (!class_exists("DataBase"))    include_once 'classes/class.DataBase.php';
            if (!class_exists("Usuario"))     include_once 'classes/class.Usuario.php';

            $bd = new DataBase();

            $mail = $_POST['mail'];
            $pass = $_POST['password'];

            $user = new Usuario();
            $user->setMail($mail);
            $user->getFromBBDDByMail();
            
            //echo "checkLogin= ".$bd->checkLogin($mail, $pass)." ||| isAdmin ".$user->getAdmin()."<br>";
                    
            
            if ($bd->checkLogin($mail, $pass) && ($user->isAdmin()) ) 
            {
//               echo "WIN!<br>";
//                exit();
                $_SESSION['id']        = $user->getId();
                $_SESSION['nombre']    = $user->getNombre();
                $_SESSION['apellidos'] = $user->getApellidos();
                $_SESSION['mail']      = $user->getMail();     
                $_SESSION['fecha']     = date("Y-m-d H:i:s");
                $_SESSION['logged']    = true;
                $_SESSION['admin']     = true;
                
                $redir                 = $cfg->urlBaseBackend;
            }
            else{
//                echo "FAIL!<br>";
//                exit();
                session_destroy(); //destruimos la session previamente creada.
                $redir                 = "{$cfg->urlLoginBackend}?m=error";
            }
            //redireccionamos.
            header("location: $redir");

            break;
   
    case "cerrar_admin":
        
            session_destroy();
            header("location: {$cfg->urlLoginBackend}");
            break;
    default:
        break;
}

?>
